<?php

namespace App\SearchBuilders;

use App\Models\Category;
use App\Models\Star;
use Illuminate\Support\Str;
class MovieSearchBuilder
{
    // 初始化查询
    protected $params = [
        'index' => 'movies',
        'type'  => '_doc',
        'body'  => [
            'query' => [
                'bool' => [
                    'filter' => [],
                    'must'   => [],
                ],
            ],
            'sort' => [
                ['scores' => 'desc']
            ],
        ],
    ];
    // 添加分页查询
    public function paginate($size, $page)
    {
        $this->params['body']['from'] = ($page - 1) * $size;
        $this->params['body']['size'] = $size;

        return $this;
    }

    // 筛选需要过滤的类目
    public function onSale(Category $category)
    {
        // 过滤类目
        $this->params['body']['query']['bool']['must_not'][] = ['term' =>['category_id' => $category->id]];

        return $this;
    }

    // 按类目筛选商品
    public function category(Category $category)
    {
        return $this->params['body']['query']['bool']['filter'][] = ['term' => ['category_id' => $category->id]];
    }
    public function actor(Star $star)
    {
        return $this->params['body']['query']['bool']['filter'][] = ['term' => ['actor_id' => $star->id]];
    }
    public function director(Star $star)
    {
        return $this->params['body']['query']['bool']['filter'][] = ['term' => ['director_id' => $star->id]];
    }
    public function author(Star $star)
    {
        return $this->params['body']['query']['bool']['filter'][] = ['term' => ['author_id' => $star->id]];
    }

    public function area($area)
    {
        if ($area !== '全部') {
            return $this->params['body']['query']['bool']['filter'][] = ['term' => ['area_name.keyword' => $area]];
        }
    }
    // 添加搜索词
    public function keywords($keywords)
    {
        // 如果参数不是数组则转为数组
        $keywords = is_array($keywords) ? $keywords : [$keywords];
        foreach ($keywords as $keyword) {
            $this->params['body']['query']['bool']['must'][] = [
                'multi_match' => [
                    'query'  => $keyword,
                    'fields' => [
                        'short_name^9',
                        'name^5',
                        'outher_name^3',
                    ],
                ],
            ];
        }

        return $this;
    }
    // 按年份筛选
    public function year($year)
    {
        if (Str::startsWith($year, '--')) {
            $yearQuery = [
                'range' => [
                    'year' => [
                        'lte' => 1949
                    ]
                ]
            ];
        } else if(Str::startsWith($year, '-')) {
            $yearQuery['range']['year']['lte'] = (int) str_replace('-', '', $year) + 10;
            $yearQuery['range']['year']['gte'] = (int) str_replace('-', '', $year);
        } else {
            $yearQuery['term'] = ['year' => $year];
        }
        $this->params['body']['query']['bool']['filter'][] = $yearQuery;

    }
    // 按演员/ 导演 / 编剧 筛选
    public function star($star)
    {
        $this->params['body']['query']['bool']['must'][] = [
            'multi_match' => [
                'query'  => $star,
                'fields' => [
                    'actor_name^3',
                    'director_name^2',
                    'author_name',
                ],
            ],
        ];
    }

    // 添加排序
    public function orderBy($field, $direction)
    {
        if (!isset($this->params['body']['sort'])) {

        }
        if ($field === 'score') $field = 'scores';
        $this->params['body']['sort'] = [];
        $this->params['body']['sort'][] = [$field => $direction];

        return $this;
    }

    // 分面搜索的聚合
    public function aggregateProperties()
    {
        /*
        $this->params['body']['aggs'] = [
            'properties' => [
                'nested' => [
                    'path' => 'area',
                ],
                'aggs'   => [
                    'properties' => [
                        'terms' => [
                            'field' => 'area.name',
                        ],
                    ],
                ],
            ],
        ];

        return $this;
        */
    }

    // 返回构造好的查询参数
    public function getParams()
    {
        return $this->params;
    }
}
